package com.shadow.study.excel.controller;

import com.shadow.study.common.R;
import com.shadow.study.excel.service.ExcelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Objects;

@Controller
@RequestMapping("excel")
public class ExcelController {

    @Resource
    private ExcelService excelService;

    @PostMapping("/importData")
    public R importData(@RequestParam("excel") final MultipartFile excel) {
        if (Objects.isNull(excel) || excel.isEmpty()) {
            return R.fail();
        }
        String msg = excelService.importData(excel, null);
        return StringUtils.isEmpty(msg) ? R.success(msg) : R.fail(msg);
    }

}
